package com.example.wanghui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.wanghui.adapter.PrimeAdapter
import java.io.File
import java.io.ObjectInputStream

class HistoryActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var primes: List<Int>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        loadPrimes()
        recyclerView.adapter = PrimeAdapter(primes)
    }

    private fun loadPrimes() {
        val fileInput = File(filesDir, "primes.txt")
        if (fileInput.exists()) {
            ObjectInputStream(fileInput.inputStream()).use {
                primes = it.readObject() as List<Int>
            }
        } else {
            primes = emptyList()
        }
    }
}